package unit;

/**
 * http url 转义符
 */
public class HttpTurnClass {

    public HttpTurnClass() {
        turnArray = new TurnClass[]{
                new TurnClass(" ", "%20")
                , new TurnClass("\"", "%22")
                , new TurnClass("#", "%23")
                , new TurnClass("%", "%25")
                , new TurnClass("&", "%26")
                , new TurnClass("(", "%28")
                , new TurnClass(")", "%29")
                , new TurnClass("+", "%2B")
                , new TurnClass(",", "%2C")
                , new TurnClass("/", "%2F")
                , new TurnClass(":", "%3A")
                , new TurnClass(";", "%3B")
                , new TurnClass("<", "%3C")
                , new TurnClass("=", "%3D")
                , new TurnClass(">", "%3E")
                , new TurnClass("? ", "%3F")
                , new TurnClass("@", "%40")
                , new TurnClass("\"", "%5C")
                , new TurnClass("|", "%7C ")
        };
    }

    private final TurnClass[] turnArray;

    /**
     * @param content 原文
     * @return 转成转义符
     */
    public String toNewChar(String content) {
        for (TurnClass row :
                turnArray) {
            content = content.replace(row.getOldChar(), row.getNewChar());
        }
        return content;
    }

    /**
     * @param content 包含转义符的内容
     * @return 将转义符转成“原文”
     */
    public String toOldChar(String content) {
        for (TurnClass row :
                turnArray) {
            content = content.replace(row.getNewChar(), row.getOldChar());
        }
        return content;
    }


    private class TurnClass {
        private String oldChar;
        private String newChar;

        public TurnClass(String oldChar, String newChar) {
            this.oldChar = oldChar;
            this.newChar = newChar;
        }

        public String getOldChar() {
            return oldChar;
        }

        public void setOldChar(String oldChar) {
            this.oldChar = oldChar;
        }

        public String getNewChar() {
            return newChar;
        }

        public void setNewChar(String newChar) {
            this.newChar = newChar;
        }
    }
}
